package com.garrett.demo.sys.dao;

import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class UserDataBaseDemo implements UserDao{

    public static final String URL = "jdbc:mysql://localhost:3306/demo_sys_schema?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC";
    public static final String USER = "test";
    public static final String PASSWORD = "test123";

    private Account onlyAccount = new Account("212006677","212006677","admin");

    @Override
    public SimsStudent getUserById(String id) {
        //1.加载驱动程序
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获得数据库连接
            Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
            //3.操作数据库，实现增删改查
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess");
            //如果有数据，rs.next()返回true
            while(rs.next()){
                System.out.println(rs.getString("user_name")+" 年龄："+rs.getInt("age"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public Account getUserByAccount(String account) {
        if (onlyAccount.getAccount().equals(account)) {
            return onlyAccount;
        }
        return null;
    }

    /**
     * 注册账户信息
     *
     * @param account 账户
     */
    @Override
    public void addAccount(Account account) {

    }

    /**
     * 添加用户信息
     *
     * @param userInfo 用户信息
     */
    @Override
    public void createUser(SimsStudent userInfo) {

    }
}
